import { defineStore } from 'pinia';
import defaultSettings from '@/settings';
import { parse, stringify } from 'zipson';

// 导入 Element Plus 中英文语言包
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import en from 'element-plus/es/locale/lang/en';

// setup
export const useAppStore = defineStore(
  'app',
  () => {
    // state
    let device = ref('desktop');
    let language = ref(defaultSettings.language);

    /**
     * 根据语言标识读取对应的语言包
     */
    let locale = computed(() => {
      return language?.value == 'en' ? en : zhCn;
    });
    let sidebar = reactive({
      opened: true,
      withoutAnimation: false
    });

    // actions
    function toggleDevice(val) {
      device.value = val;
    }
    function toggleSidebar(withoutAnimation) {
      sidebar.opened = !sidebar.opened;
      sidebar.withoutAnimation = withoutAnimation;
    }

    /**
     * 切换语言
     *
     * @param val
     */
    function changeLanguage(val) {
      language.value = val;
    }

    return {
      device,
      language,
      locale,
      sidebar,
      toggleDevice,
      toggleSidebar,
      changeLanguage
    };
  },
  {
    persist: {
      paths: ['device', 'language'],
      serializer: {
        deserialize: parse,
        serialize: stringify
      }
    }
  }
);
